package com.bawei.house


import android.app.NotificationChannel
import android.app.NotificationManager


import android.content.Context

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import androidx.core.view.isVisible
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.bean.HouseEntity
import com.bawei.base.mvvm.BaseActivity
import com.bawei.house.adapter.HouseAdapter
import com.bawei.house.adapter.HouseTypeAdapter
import com.bawei.house.databinding.ActivityHouseBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/house/HouseActivity")
@AndroidEntryPoint
class HouseActivity : BaseActivity<ActivityHouseBinding, HouseViewModel>() {
    private lateinit var houseTypeAdapter: HouseTypeAdapter
    private var index = 0
    private lateinit var houseAdapter: HouseAdapter
    private val houseList = mutableListOf<HouseEntity>()

    @RequiresApi(Build.VERSION_CODES.O)
    override fun initView() {
        val areaList = mutableListOf("A区", "B区", "C区", "D区")
        // 创建一个通知渠道，用于Android Oreo及更高版本
        val channel =
            NotificationChannel("channelId", "channelName", NotificationManager.IMPORTANCE_DEFAULT)
        // 获取系统的NotificationManager服务
        // 在Android Oreo及更高版本中，通知必须与一个渠道关联，这里我们创建一个默认重要性的渠道
        getSystemService(NotificationManager::class.java).createNotificationChannel(channel)
        // 3. 构建通知
        val notification = NotificationCompat.Builder(this, "channelId")
            .setSmallIcon(android.R.drawable.ic_dialog_info) // 必须设置的小图标
            .setContentTitle("我的通知标题")
            .setContentText("这是一条简单的通知内容")
            .build()
        (getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).notify(
            1,
            notification
        ) // 使用唯一ID（这里用1）


        houseAdapter = HouseAdapter()
        houseTypeAdapter = HouseTypeAdapter()
        houseTypeAdapter.data.clear()
        viewModel.house()
        houseTypeAdapter.data.addAll(areaList)
        binding.huoseType.adapter = houseTypeAdapter
        binding.huoseType.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        houseTypeAdapter.setOnItemClickListener { _, _, position ->
            index = houseTypeAdapter.setSelectId(areaList[position], index, position)
            when (position) {
                0 -> {
                    binding.linOne.isVisible = true
                    binding.linTwo.isVisible = true
                    binding.linThree.isVisible = true
                }
                1 -> {
                    binding.linOne.isVisible = false
                    binding.linTwo.isVisible = false
                    binding.linThree.isVisible = false
                }
                2 -> {
                    binding.linOne.isVisible = false
                    binding.linTwo.isVisible = false
                    binding.linThree.isVisible = false
                }
                3 -> {
                    binding.linOne.isVisible = false
                    binding.linTwo.isVisible = false
                    binding.linThree.isVisible = false
                }
            }
        }
        binding.huosrSquareOne.setOnClickListener {
            houseAdapter.data.clear()
            houseList.forEach {
                if (it.regionId == 1) {
                    houseAdapter.data.add(it)
                }
            }
            binding.houseRecOne.adapter = houseAdapter
            binding.houseRecOne.layoutManager = GridLayoutManager(this, 2)
            binding.houseRecOne.isVisible = true
            binding.houseRecTwo.isVisible = false
            binding.houseRecThree.isVisible = false
        }
        binding.huosrSquareTwo.setOnClickListener {
            houseAdapter.data.clear()
            houseList.forEach {
                if (it.regionId == 2) {

                    houseAdapter.data.add(it)
                }
            }
            binding.houseRecTwo.adapter = houseAdapter
            binding.houseRecTwo.layoutManager = GridLayoutManager(this, 2)
            binding.houseRecTwo.isVisible = true
            binding.houseRecOne.isVisible = false
            binding.houseRecThree.isVisible = false
        }
        binding.huosrSquareThree.setOnClickListener {
            houseAdapter.data.clear()
            houseList.forEach {
                if (it.regionId == 3) {
                    houseAdapter.data.add(it)
                }
            }
            binding.houseRecThree.adapter = houseAdapter
            binding.houseRecThree.layoutManager = GridLayoutManager(this, 2)
            binding.houseRecThree.isVisible = true
            binding.houseRecOne.isVisible = false
            binding.houseRecTwo.isVisible = false
        }
        binding.firmBack.setOnClickListener {
            ARouter.getInstance().build("/house/LesseeActivity").navigation()
        }
    }

    override fun initData() {
        viewModel.houseSuccess.observe(this) {
            houseList.clear()
            houseList.addAll(it)
        }
    }
}